<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解Java锁机制</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        /* 首字下沉效果 */
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #4a5568;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 代码块美化 */
        pre {
            background: #1e293b;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            position: relative;
        }
        
        pre::before {
            content: "JAVA";
            position: absolute;
            top: 0.5rem;
            right: 1rem;
            font-size: 0.75rem;
            color: #64748b;
            font-weight: 600;
            letter-spacing: 0.1em;
        }
        
        code {
            color: #e2e8f0;
            font-family: 'Fira Code', monospace;
            font-size: 0.9rem;
            line-height: 1.6;
        }
        
        /* 卡片悬停效果 */
        .card-hover {
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }
        
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
        }
        
        /* 标题装饰线 */
        .title-decoration::after {
            content: '';
            display: block;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #3b82f6 0%, #8b5cf6 100%);
            margin-top: 1rem;
            border-radius: 2px;
        }
        
        /* 引用样式 */
        .quote-box {
            border-left: 4px solid #3b82f6;
            background: rgba(59, 130, 246, 0.05);
            padding: 1.5rem;
            margin: 2rem 0;
            border-radius: 0 8px 8px 0;
            position: relative;
        }
        
        .quote-box::before {
            content: '\f10d';
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            position: absolute;
            top: -10px;
            left: 20px;
            font-size: 2rem;
            color: #3b82f6;
            opacity: 0.2;
        }
        
        /* 列表美化 */
        .custom-list li {
            position: relative;
            padding-left: 2rem;
            margin-bottom: 1rem;
        }
        
        .custom-list li::before {
            content: '\f054';
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            position: absolute;
            left: 0;
            top: 0.25rem;
            color: #3b82f6;
            font-size: 0.75rem;
        }
        
        /* 图片容器 */
        .image-container {
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin: 2rem 0;
        }
        
        /* 滚动动画 */
        .fade-in {
            opacity: 0;
            transform: translateY(20px);
            animation: fadeIn 0.8s ease-out forwards;
        }
        
        @keyframes fadeIn {
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        /* Mermaid图表样式 */
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="relative overflow-hidden bg-gradient-to-br from-blue-600 via-purple-600 to-pink-500 text-white">
        <div class="absolute inset-0 bg-black opacity-20"></div>
        <div class="relative container mx-auto px-6 py-24 text-center">
            <h1 class="text-5xl md:text-7xl font-bold mb-6 animate-pulse">
                <i class="fas fa-lock mr-4"></i>Java锁机制深度解析
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                探索多线程编程中的核心概念，掌握并发控制的精髓
            </p>
            <div class="flex justify-center space-x-4 text-sm">
                <span><i class="fas fa-calendar-alt mr-2"></i>2024年4月</span>
                <span><i class="fas fa-clock mr-2"></i>阅读时间 15分钟</span>
                <span><i class="fas fa-tag mr-2"></i>Java并发编程</span>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0">
            <svg viewBox="0 0 1440 120" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 120L60 105C120 90 240 60 360 45C480 30 600 30 720 37.5C840 45 960 60 1080 67.5C1200 75 1320 75 1380 75L1440 75V120H1380C1320 120 1200 120 1080 120C960 120 840 120 720 120C600 120 480 120 360 120C240 120 120 120 60 120H0Z" fill="white"/>
            </svg>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        
        <!-- 锁机制概述 -->
        <section class="mb-16 fade-in">
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <h2 class="text-3xl font-bold text-gray-800 mb-6 title-decoration">
                    <i class="fas fa-shield-alt mr-3 text-blue-500"></i>什么是锁机制
                </h2>
                <p class="text-lg text-gray-700 leading-relaxed drop-cap">
                    通过锁机制，能够保证在多线程环境中，在某一个时间点上，只能有一个线程进入临界区代码，从而保证临界区中操作数据的一致性。锁可以理解为内存中的一个整型数，拥有两种状态：空闲状态和上锁状态。
                </p>
                <div class="mt-6 image-container">
                    <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1714026400319-760ad8bb-5998-48b3-9233-1c2271c46f0f.png" 
                         alt="锁机制示意图" 
                         class="w-full">
                </div>
            </div>
        </section>

        <!-- 为什么需要锁 -->
        <section class="mb-16 fade-in" style="animation-delay: 0.2s">
            <div class="bg-gradient-to-r from-purple-50 to-blue-50 rounded-2xl shadow-xl p-8 card-hover">
                <h2 class="text-3xl font-bold text-gray-800 mb-6 title-decoration">
                    <i class="fas fa-question-circle mr-3 text-purple-500"></i>为什么要使用锁
                </h2>
                <div class="quote-box">
                    <p class="text-lg text-gray-700">
                        在多线程情况下完成操作时，由于并不是原子操作，所以在完成操作的过程中可能会被打断，造成数据的不一致性。比如超卖问题。
                    </p>
                </div>
                <div class="mt-6">
                    <pre><code>package com.qf.day30lock01hello.base;

import lombok.Data;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 验证i++是线程不安全
 */
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        User user = new User();
        for (int i = 0; i < 500; i++) {
            new Thread(() -> {
                int age = user.getAge().incrementAndGet();
                try {
                    Thread.sleep(10 * new Random().nextInt(3));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        Thread.sleep(5000);
        System.out.println(user.getAge().get());
    }
}

@Data
class User {
    private AtomicInteger age = new AtomicInteger(0);
}</code></pre>
                </div>
            </div>
        </section>

        <!-- 锁的分类可视化 -->
        <section class="mb-16 fade-in" style="animation-delay: 0.4s">
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <h2 class="text-3xl font-bold text-gray-800 mb-8 title-decoration text-center">
                    <i class="fas fa-sitemap mr-3 text-green-500"></i>锁的分类体系
                </h2>
                <div class="mermaid">
                    graph TD
                        A[Java锁机制] --> B[乐观锁/悲观锁]
                        A --> C[独享锁/共享锁]
                        A --> D[互斥锁/读写锁]
                        A --> E[可重入锁/不可重入锁]
                        A --> F[公平锁/非公平锁]
                        A --> G[分段锁/自旋锁]
                        A --> H[CAS/AQS]
                        
                        B --> B1[乐观锁<br/>CAS机制<br/>版本号机制]
                        B --> B2[悲观锁<br/>synchronized<br/>ReentrantLock]
                        
                        C --> C1[独享锁<br/>ReentrantLock]
                        C --> C2[共享锁<br/>ReadWriteLock读锁]
                        
                        E --> E1[可重入锁<br/>synchronized<br/>ReentrantLock]
                        E --> E2[不可重入锁<br/>自定义锁]
                        
                        style A fill:#f9f,stroke:#333,stroke-width:4px
                        style B fill:#bbf,stroke:#333,stroke-width:2px
                        style C fill:#bbf,stroke:#333,stroke-width:2px
                        style D fill:#bbf,stroke:#333,stroke-width:2px
                        style E fill:#bbf,stroke:#333,stroke-width:2px
                        style F fill:#bbf,stroke:#333,stroke-width:2px
                        style G fill:#bbf,stroke:#333,stroke-width:2px
                        style H fill:#bbf,stroke:#333,stroke-width:2px
                </div>
            </div>
        </section>

        <!-- 锁的详细介绍 -->
        <section class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-16">
            
            <!-- 乐观锁/悲观锁 -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover fade-in" style="animation-delay: 0.6s">
                <h3 class="text-2xl font-bold text-gray-800 mb-4">
                    <i class="fas fa-smile mr-2 text-yellow-500"></i>乐观锁 vs 悲观锁
                </h3>
                <div class="space-y-4">
                    <div class="bg-green-50 p-4 rounded-lg">
                        <h4 class="font-semibold text-green-800 mb-2">乐观锁</h4>
                        <p class="text-gray-700">认为不存在并发问题，不加锁，更新时判断数据是否被修改。适用于读多写少的场景。</p>
                        <ul class="custom-list mt-3 text-sm text-gray-600">
                            <li>CAS（Compare And Swap）机制</li>
                            <li>版本号机制（Version）</li>
                            <li>时间戳机制（Timestamp）</li>
                        </ul>
                    </div>
                    <div class="bg-red-50 p-4 rounded-lg">
                        <h4 class="font-semibold text-red-800 mb-2">悲观锁</h4>
                        <p class="text-gray-700">认为一定存在并发问题，每次都加锁。适用于写多读少的场景。</p>
                        <ul class="custom-list mt-3 text-sm text-gray-600